# -*- coding: utf-8 -*-
import cgi
import os
import sys
import datetime
import time
import wsgiref.handlers
import string

from control import *

from time import strftime, localtime 
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from loginTool import login_required

class Reply(db.Model): #//用户回复表
      userName = db.StringProperty(multiline=True)#//用户名
      maincode  = db.StringProperty(multiline=True) #存第一级内容编号，后面的表都加上
      code = db.StringProperty(multiline=True)#//所回复的内容编码
      replyContent = db.StringProperty(multiline=True)#//回复内容
      replyType = db.StringProperty(multiline=True)#//回复类型
      replyTime = db.DateTimeProperty(auto_now_add=True)#//回复时间

class PublicPage(webapp.RequestHandler):
  def render(self, template_file, template_value):
      path = os.path.join(os.path.dirname(__file__), template_file)
      self.response.out.write(template.render(path, template_value))


class UserReplyList(PublicPage):
  @login_required
  def get(self,page):
      searchType = self.request.get('searchType')
      if searchType=='':
          replys = db.GqlQuery("SELECT * FROM Reply ORDER BY replyTime DESC")
      else:
          replys = db.GqlQuery("SELECT * FROM Reply WHERE userName=:1 ORDER BY replyTime DESC",searchType)
          
      index=0 if page=="" else int(page)
      replys = replys.fetch(16,index*16)
      prev,next=getPageing(len(replys), index)
      template_values = {"replys":replys[:16],"prev":prev,"next":next,"index":index,}
      self.render('template/reply/replyList.html',template_values)
#      self.render('html/replyManage/replyList.html',template_values)
  @login_required
  def post(self,page):
      searchType = self.request.get('searchType')
      self.redirect('/userReplyList/%s?searchType=%s' %(page,searchType))

class UserReplyAdd(PublicPage):
  @login_required
  def get(self):
      #获取资讯的主级编码以及内容编码#
      contents = db.GqlQuery("SELECT * FROM ContentClass WHERE level =:1 ORDER BY code ASC",'1')
      template_values = {"contents":contents,}
      self.render('template/reply/replyListAdd.html',template_values)
#      self.render('html/replyManage/replyListAdd.html',template_values)
  @login_required
  def post(self):
      greeting = Reply()
      greeting.maincode = self.request.get('maincode')
      userName = greeting.userName = self.request.get('userName')
      greeting.code = self.request.get('code')
      greeting.replyType = self.request.get('replyType')
      greeting.replyContent = self.request.get('replyContent')
      greeting.put()
      self.redirect('/userReplyList/0?searchType=%s' %userName)

class UserReplyDelete(PublicPage):
  @login_required
  def get(self):
      userName = self.request.get('userName')
#      code = self.request.get('code')
      id = self.request.get('id')#2
      replys=Reply.get_by_id(int(id))
      index = self.request.get('index')
#      replys = db.GqlQuery("SELECT * FROM Reply WHERE userName=:1 AND code=:2",userName,code)
      if replys:
          db.delete(replys)
      self.redirect('/userReplyList/%s?' %index)

class UserReplyUpdate(PublicPage):
  @login_required
  def get(self):
      userName = self.request.get('userName')#1
#      code = self.request.get('code')#2
      index = self.request.get('index')#3
      id = self.request.get('id')#2
      replys=Reply.get_by_id(int(id))
#      replys = db.GqlQuery("SELECT * FROM Reply WHERE userName=:1 AND code=:2 ORDER BY replyTime DESC",userName,code)
      template_values={"replys":[replys],"index":index,}
      self.render('template/reply/replyListUpdate.html', template_values)
#      self.render('html/replyManage/replyListUpdate.html', template_values)
  @login_required
  def post(self):
      index = self.request.get('index')
      userName = self.request.get('userName')#1
#      code = self.request.get('code')#3
      id = self.request.get('id')#2
      replys=Reply.get_by_id(int(id))
#      replys = db.GqlQuery("SELECT * FROM Reply WHERE userName=:1 AND code=:2",userName,code)
      if replys:
          replys.replyType = self.request.get('replyType')#4
          replys.replyContent = self.request.get('replyContent')#5
#          replys.replyTime = datetime.datetime.now()
          db.put(replys)
      self.redirect('/userReplyList/%s?searchType=%s' %(index,userName))
